package com.example.multinetty.netty.thirdprotocal.controller;


import com.example.multinetty.netty.thirdprotocal.ChatSession;
import com.example.multinetty.netty.thirdprotocal.ErrorCodeException;
import com.example.multinetty.netty.thirdprotocal.Result;
import com.example.multinetty.netty.thirdprotocal.ResultCode;
import com.example.multinetty.netty.thirdprotocal.dao.Player;
import com.example.multinetty.netty.thirdprotocal.service.ChatService;
import com.example.multinetty.netty.thirdprotocal.vo.chat.ChatResponse;
import com.example.multinetty.netty.thirdprotocal.vo.chat.PrivateChatRequest;
import com.example.multinetty.netty.thirdprotocal.vo.chat.PublicChatRequest;
import com.example.multinetty.netty.thirdprotocal.vo.player.LoginRequest;
import com.example.multinetty.netty.thirdprotocal.vo.player.PlayerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/chatonline")
public class ChatController {
	
	@Autowired
	private ChatService chatService;

	@PostMapping(value = "/publicChat")
	public Result<?> publicChat(@RequestBody PublicChatRequest  publicChatRequest, HttpServletRequest request) {
	/*public Result<?> publicChat(long playerId, byte[] data) {*/
		ChatResponse result = null;
		ChatSession chatSession=(ChatSession)request.getAttribute("session");
		long playerId= ((Player) chatSession.getAttachment()).getPlayerId();
		try {
			//反序列化
			/*PublicChatRequest request = new PublicChatRequest();
			request.readFromBytes(data);*/
			
			//参数校验
			if(StringUtils.isEmpty(publicChatRequest.getContext())){
				return Result.ERROR(ResultCode.AGRUMENT_ERROR);
			}
			
			//执行业务
			chatService.publicChat(playerId, publicChatRequest.getContext());
		} catch (ErrorCodeException e) {
			return Result.ERROR(e.getErrorCode());
		} catch (Exception e) {
			e.printStackTrace();
			return Result.ERROR(ResultCode.UNKOWN_EXCEPTION);
		}
		return Result.SUCCESS();
	}
	@PostMapping(value = "/privateChat")
	public Result<?> privateChat(@RequestBody PrivateChatRequest privateChatRequest, HttpServletRequest request/*long playerId, byte[] data)*/) {
		ChatResponse result = null;
		try {
			//反序列化
			/*PrivateChatRequest request = new PrivateChatRequest();
			request.readFromBytes(data);
			*/
			//参数校验
			if(StringUtils.isEmpty(privateChatRequest.getContext()) || privateChatRequest.getTargetPlayerId() <= 0){
				return Result.ERROR(ResultCode.AGRUMENT_ERROR);
			}

			ChatSession chatSession=(ChatSession)request.getAttribute("session");
			long playerId= (long) chatSession.getAttachment();
			//执行业务
			chatService.privateChat(playerId, privateChatRequest.getTargetPlayerId(), privateChatRequest.getContext());
		} catch (ErrorCodeException e) {
			return Result.ERROR(e.getErrorCode());
		} catch (Exception e) {
			e.printStackTrace();
			return Result.ERROR(ResultCode.UNKOWN_EXCEPTION);
		}
		return Result.SUCCESS();
	}
}
